package com.xckj.zuoliao.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.zuoliao.R;
import com.xckj.zuoliao.bean.ListBean;
import com.xckj.zuoliao.global.Constant;

import java.util.List;

/**
 * @Author: 代码的搬运工
 * @Describe: 澳门列表
 * @Date: 2024/3/29 18:55
 */
public class MacaoListAdapter extends RecyclerView.Adapter {
    private List<ListBean> listBeans;
    private OnItemClickListener onItemClickListener;
    private final Context context;

    public MacaoListAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<ListBean> listBeans) {
        this.listBeans = listBeans;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_macao_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(view, position);
            }
        });
        ListBean listBean = listBeans.get(position);
        itemViewHolder.tvTitle1.setText(listBean.name);
        itemViewHolder.tvTitle2.setText(listBean.title);
        setTextColor(itemViewHolder, listBean);
    }

    private void setTextColor(ItemViewHolder itemViewHolder, ListBean listBean) {
        if (Constant.TYPE_1.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_9));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_9));
        } else if (Constant.TYPE_2.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_2));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_2));
        } else if (Constant.TYPE_3.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_3));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_3));
        } else if (Constant.TYPE_4.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_2));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_2));
        } else if (Constant.TYPE_5.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_4));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_4));
        } else if (Constant.TYPE_6.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_5));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_5));
        } else if (Constant.TYPE_7.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_3));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_3));
        } else if (Constant.TYPE_8.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_6));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_6));
        } else if (Constant.TYPE_9.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_7));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_7));
        } else if (Constant.TYPE_10.equals(listBean.colum)) {
            itemViewHolder.tvTitle1.setTextColor(ContextCompat.getColor(context, R.color.item_color_8));
            itemViewHolder.tvTitle2.setTextColor(ContextCompat.getColor(context, R.color.item_color_8));
        }
    }

    @Override
    public int getItemCount() {
        return listBeans == null ? 0 : listBeans.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle1;
        private final TextView tvTitle2;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle1 = (TextView) itemView.findViewById(R.id.tv_title_1);
            tvTitle2 = (TextView) itemView.findViewById(R.id.tv_title_2);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item子View的点击事件
         *
         * @param view     当前视图
         * @param position 当前索引
         */
        void onItemClick(View view, int position);
    }
}
